/**
 * Created by wry on 2015/2/12.
 */
'use strict';

angular.module('backofficeApp.voucherManager.controllers', []).
    controller('voucherTypeCtrl', ['$scope', '$http', '$location', function ($scope, $http, $location) {

        $scope.type = [
            {
                name: '代金券',
                value: 1
            },
            {
                name: '情值',
                value: 2
            }
        ];

        $scope.getVoucherType = function () {
            $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/getVoucherType')
                .success(function (data) {
                    $scope.voucherType = data.data;
                }).error(function () {
                    $scope.voucherType = null;
                });
        }

        $scope.getVoucherType();

        $scope.addVoucherType = function () {
            $scope.valueErrMsg = null;
            $('#d_value').removeClass("has-error");
            $('#voucher_model').modal('show');
        };

        $scope.saveVoucher = function (voucher) {

            if (voucher == null || voucher.voucher_name == null || voucher.voucher_val == null || voucher.type == null) {
                return;
            }
            voucher.voucher_val = Number(voucher.voucher_val);

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/addVoucherType', voucher, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                if (data.result.status == 1) {
                    $scope.getVoucherType();
                    $('#voucher_model').modal('hide');
                } else {
                    $scope.valueErrMsg = '同类型代金券不能重复';
                    $('#d_value').addClass("has-error")
                }
            }).error(function () {

            });
        }
    }])
    .controller('voucherListCtrl', ['$scope', '$http', '$location', 'CommunityService', function ($scope, $http, $location, communityService) {

        $scope.currentPage = 1;
        $scope.totalPage = 0;
        $scope.pages = [];

        $scope.useStatus = [{
            value: 0,
            name: '未使用'
        }, {
            value: 1,
            name: '已使用'
        }];

        $scope.reward = [{
            value: 1,
            name: 'v认证'
        }, {
            value: 2,
            name: '推荐用户'
        }];

        function datePicker(id) {
            function d(id) {
                var self = this;
                $(id).datepicker({
                    format: 'yyyy-mm-dd',
                    autoclose: true,
                    forceParse: false
                }).on('changeDate', function (e) {
                    self.date = e.date;
                }).on('show', function (e) {
                    $(".datepicker.datepicker-dropdown.dropdown-menu").css("z-index", "10000");

                });

            }

            d.prototype.getValue = function (isFrom) {
                if ($(id).val() == "") {
                    return ''
                }

                if (this.date == null) {
                    return '';
                }
                if (isFrom) {
                    return this.date.setHours(0, 0, 0, 0)
                }
                return this.date.setHours(23, 59, 59, 0)
            }
            var date = new d(id);
            return date;
        }

        $scope.startDate = datePicker('#startDate');
        $scope.endDate = datePicker('#endDate');


        $scope.getVoucherList = function (opt) {

            if (opt == null) {
                opt = {}
            }
            opt.startTime = $scope.startDate.getValue(true);
            opt.endTime = $scope.endDate.getValue(false);

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/searchVoucher/' + $scope.currentPage, opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.voucherList = data.data.list;
                $scope.totalPage = data.data.totalPage;
            }).error(function () {
                $scope.voucherList = null;
            });
        };

        $scope.getVoucherList();

        $scope.onSelectPage = function (page) {
            $scope.currentPage = page;
            $scope.getVoucherList($scope.opt);
        }


    }]).directive('mutau', function () {
        return {
            restrict: 'E',
            template: '',
            replace: true,
            link: function (scope, element, attrs) {
                scope.$watch('currentPage', function (value) {
                    scope.pages = [];
                    for (var i = value - 5 > 0 ? value - 5 : 1; i <= (value + 5 > scope.totalPage ? scope.totalPage : value + 5); i++) {
                        scope.pages.push(i);
                    }
                });
                scope.$watch('totalPage', function (value) {
                    scope.pages = [];
                    for (var i = scope.currentPage - 5 > 0 ? scope.currentPage - 5 : 1; i <= (scope.currentPage + 5 > value ? value : scope.currentPage + 5); i++) {
                        scope.pages.push(i);
                    }
                });

                scope.isActive = function (page) {
                    return scope.currentPage === page;
                };
                scope.selectPage = function (page) {
                    if (!scope.isActive(page)) {
                        scope.currentPage = page;
                        scope.onSelectPage(page);
                    }
                };
                scope.selectPrevious = function () {
                    if (!scope.noPrevious()) {
                        scope.selectPage(scope.currentPage - 1);
                    }
                };
                scope.selectNext = function () {
                    if (!scope.noNext()) {
                        scope.selectPage(scope.currentPage + 1);
                    }
                };
                scope.noPrevious = function () {
                    return scope.currentPage == 1;
                };
                scope.noNext = function () {
                    return scope.currentPage == scope.totalPage;
                };
            }
        };
    }).controller('voucherStatisticsCtrl', ['$scope', '$http', '$location', 'CommunityService', function ($scope, $http, $location, communityService) {

        $scope.communityList = communityService.getAllCommunityService();

        $scope.currentPage = 1;
        $scope.totalPage = 0;
        $scope.pages = [];

        $scope.voucher_status = [{
            value:1,
            name:"V认证"
        },{
            value:2,
            name:"推荐用户"
        }];

        $scope.voucherStatusList = [{
            value:0,
            name:"已使用"
        },{
            value:1,
            name:"未使用"
        }];

        function datePicker(id) {
            function d(id) {
                var self = this;
                $(id).datepicker({
                    format: 'yyyy-mm-dd',
                    autoclose: true,
                    forceParse: false
                }).on('changeDate', function (e) {
                    self.date = e.date;
                }).on('show', function (e) {
                    $(".datepicker.datepicker-dropdown.dropdown-menu").css("z-index", "10000");

                });

            }

            d.prototype.getValue = function (isFrom) {
                if ($(id).val() == null || $(id).val() == "") {
                    return ''
                }

                if (this.date == null) {
                    return '';
                }
                if (isFrom) {
                    return this.date.setHours(0, 0, 0, 0)
                }
                return this.date.setHours(23, 59, 59, 0)
            }
            var date = new d(id);
            return date;
        }

        $scope.beginTimeTmp = datePicker('#beginTime');
        $scope.endTimeTmp = datePicker('#endTime');


        $scope.getVoucherList = function (opt) {

            var params = {
                communityName:$scope.filter_community_name,
                voucherType:$scope.filter_voucher_type,
                rewardSource:$scope.voucher_reward_source,
                isUse:$scope.filter_is_use,
                excludeUser:$scope.excludeUser,
                currentPage:$scope.currentPage
            };

            params.beginTime = $scope.beginTimeTmp.getValue(true);
            params.endTime = $scope.endTimeTmp.getValue(false);

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/getVoucherStatistics/', params, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.voucherList = data.data.list;
                $scope.totalPage = data.data.totalPage;
            }).error(function () {
                $scope.voucherList = null;
            });
        };


        $scope.getVoucherList();

        $scope.onSelectPage = function (page) {
            $scope.currentPage = page;
            $scope.getVoucherList($scope.opt);
        }

        $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/getVoucherType', {
            headers: {'Content-Type': 'application/json;charset=utf-8'}
        }).success(function (data) {
            $scope.voucherTypeList = data;
        })

    }]).directive('mutau', function () {
        return {
            restrict: 'E',
            template: '',
            replace: true,
            link: function (scope, element, attrs) {
                scope.$watch('currentPage', function (value) {
                    scope.pages = [];
                    for (var i = value - 5 > 0 ? value - 5 : 1; i <= (value + 5 > scope.totalPage ? scope.totalPage : value + 5); i++) {
                        scope.pages.push(i);
                    }
                });
                scope.$watch('totalPage', function (value) {
                    scope.pages = [];
                    for (var i = scope.currentPage - 5 > 0 ? scope.currentPage - 5 : 1; i <= (scope.currentPage + 5 > value ? value : scope.currentPage + 5); i++) {
                        scope.pages.push(i);
                    }
                });

                scope.isActive = function (page) {
                    return scope.currentPage === page;
                };
                scope.selectPage = function (page) {
                    if (!scope.isActive(page)) {
                        scope.currentPage = page;
                        scope.onSelectPage(page);
                    }
                };
                scope.selectPrevious = function () {
                    if (!scope.noPrevious()) {
                        scope.selectPage(scope.currentPage - 1);
                    }
                };
                scope.selectNext = function () {
                    if (!scope.noNext()) {
                        scope.selectPage(scope.currentPage + 1);
                    }
                };
                scope.noPrevious = function () {
                    return scope.currentPage == 1;
                };
                scope.noNext = function () {
                    return scope.currentPage == scope.totalPage;
                };
            }
        };
    }).controller('voucherConsumeCtrl', ['$scope', '$http', '$location', 'CommunityService', function ($scope, $http, $location, communityService) {

        $scope.currentPage = 1;
        $scope.totalPage = 0;
        $scope.pages = [];

        function datePicker(id) {
            function d(id) {
                var self = this;
                $(id).datepicker({
                    format: 'yyyy-mm-dd',
                    autoclose: true,
                    forceParse: false
                }).on('changeDate', function (e) {
                    self.date = e.date;
                }).on('show', function (e) {
                    $(".datepicker.datepicker-dropdown.dropdown-menu").css("z-index", "10000");

                });

            }

            d.prototype.getValue = function (isFrom) {
                if ($(id).val() == null || $(id).val() == "") {
                    return ''
                }

                if (this.date == null) {
                    return '';
                }
                if (isFrom) {
                    return this.date.setHours(0, 0, 0, 0)
                }
                return this.date.setHours(23, 59, 59, 0)
            }
            var date = new d(id);
            return date;
        }

        $scope.beginTimeTmp = datePicker('#beginTime');
        $scope.endTimeTmp = datePicker('#endTime');


        $scope.getVoucherList = function (opt) {

            var params = {
                retailerName:$scope.filter_retailer_name,
                voucherType:$scope.filter_voucher_type,
                currentPage:$scope.currentPage
            };


            params.beginTime = $scope.beginTimeTmp.getValue(true);
            params.endTime = $scope.endTimeTmp.getValue(false);

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/getConsumeStatistics/', params, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.voucherList = data.data.list;
                $scope.totalPage = data.data.totalPage;
            }).error(function () {
                $scope.voucherList = null;
            });
        };


        $scope.getVoucherList();

        $scope.onSelectPage = function (page) {
            $scope.currentPage = page;
            $scope.getVoucherList($scope.opt);
        }

        $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/getVoucherType', {
            headers: {'Content-Type': 'application/json;charset=utf-8'}
        }).success(function (data) {
            $scope.voucherTypeList = data;
        })

    }]).directive('mutau', function () {
        return {
            restrict: 'E',
            template: '',
            replace: true,
            link: function (scope, element, attrs) {
                scope.$watch('currentPage', function (value) {
                    scope.pages = [];
                    for (var i = value - 5 > 0 ? value - 5 : 1; i <= (value + 5 > scope.totalPage ? scope.totalPage : value + 5); i++) {
                        scope.pages.push(i);
                    }
                });
                scope.$watch('totalPage', function (value) {
                    scope.pages = [];
                    for (var i = scope.currentPage - 5 > 0 ? scope.currentPage - 5 : 1; i <= (scope.currentPage + 5 > value ? value : scope.currentPage + 5); i++) {
                        scope.pages.push(i);
                    }
                });

                scope.isActive = function (page) {
                    return scope.currentPage === page;
                };
                scope.selectPage = function (page) {
                    if (!scope.isActive(page)) {
                        scope.currentPage = page;
                        scope.onSelectPage(page);
                    }
                };
                scope.selectPrevious = function () {
                    if (!scope.noPrevious()) {
                        scope.selectPage(scope.currentPage - 1);
                    }
                };
                scope.selectNext = function () {
                    if (!scope.noNext()) {
                        scope.selectPage(scope.currentPage + 1);
                    }
                };
                scope.noPrevious = function () {
                    return scope.currentPage == 1;
                };
                scope.noNext = function () {
                    return scope.currentPage == scope.totalPage;
                };
            }
        };
    }).controller('voucherIsSettlementCtrl', ['$scope', '$http', '$location', 'CommunityService', function ($scope, $http, $location, communityService) {

        $scope.currentPage = 1;
        $scope.totalPage = 0;
        $scope.pages = [];

        function datePicker(id) {
            function d(id) {
                var self = this;
                $(id).datepicker({
                    format: 'yyyy-mm-dd',
                    autoclose: true,
                    forceParse: false
                }).on('changeDate', function (e) {
                    self.date = e.date;
                }).on('show', function (e) {
                    $(".datepicker.datepicker-dropdown.dropdown-menu").css("z-index", "10000");

                });

            }

            d.prototype.getValue = function (isFrom) {
                if ($(id).val() == null || $(id).val() == "") {
                    return ''
                }

                if (this.date == null) {
                    return '';
                }
                if (isFrom) {
                    return this.date.setHours(0, 0, 0, 0)
                }
                return this.date.setHours(23, 59, 59, 0)
            }
            var date = new d(id);
            return date;
        }

        $scope.beginTimeTmp = datePicker('#beginTime');
        $scope.endTimeTmp = datePicker('#endTime');


        $scope.getVoucherList = function (opt) {

            var params = {
                retailerName:$scope.filter_retailer_name,
                currentPage:$scope.currentPage
            };

            params.beginTime = $scope.beginTimeTmp.getValue(true);
            params.endTime = $scope.endTimeTmp.getValue(false);

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/getIsSettlementProfile/', params, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.voucherList = data.data.list;
                $scope.totalPage = data.data.totalPage;
            }).error(function () {
                $scope.voucherList = null;
            });
        };


        $scope.getVoucherList();

        $scope.onSelectPage = function (page) {
            $scope.currentPage = page;
            $scope.getVoucherList($scope.opt);
        }

    }]).directive('mutau', function () {
        return {
            restrict: 'E',
            template: '',
            replace: true,
            link: function (scope, element, attrs) {
                scope.$watch('currentPage', function (value) {
                    scope.pages = [];
                    for (var i = value - 5 > 0 ? value - 5 : 1; i <= (value + 5 > scope.totalPage ? scope.totalPage : value + 5); i++) {
                        scope.pages.push(i);
                    }
                });
                scope.$watch('totalPage', function (value) {
                    scope.pages = [];
                    for (var i = scope.currentPage - 5 > 0 ? scope.currentPage - 5 : 1; i <= (scope.currentPage + 5 > value ? value : scope.currentPage + 5); i++) {
                        scope.pages.push(i);
                    }
                });

                scope.isActive = function (page) {
                    return scope.currentPage === page;
                };
                scope.selectPage = function (page) {
                    if (!scope.isActive(page)) {
                        scope.currentPage = page;
                        scope.onSelectPage(page);
                    }
                };
                scope.selectPrevious = function () {
                    if (!scope.noPrevious()) {
                        scope.selectPage(scope.currentPage - 1);
                    }
                };
                scope.selectNext = function () {
                    if (!scope.noNext()) {
                        scope.selectPage(scope.currentPage + 1);
                    }
                };
                scope.noPrevious = function () {
                    return scope.currentPage == 1;
                };
                scope.noNext = function () {
                    return scope.currentPage == scope.totalPage;
                };
            }
        };
    });

